package mainpackage;

import engine.net.NetManager;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * <p>Title: 内部消息类</p>
 * <p>Description: 主要负责处理游戏内各个耗时功能模块或指令等任务逻辑处理</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: </p>
 * @author hlw
 * @version 1.0
 */
public class LocalMsgManager implements Runnable {
    //内部消息队列

    private Vector msgAlignment;
    //内部消息id
    public byte msgId;
    //内部消息线程停止运行开关
    private boolean msgStopRun;
    //本类实例
    public static LocalMsgManager instance;
    //进入新区域数据包
    public byte[] byEnterNewMap;
    //玩家复活数据包
    public byte[] byReliveData;
    public Image loadingPic;
    public static Hashtable netMsgHash; //存储网络消息的hash

    /**
     * 实例化本类单例
     * @return LocalMsgManager 本类实例
     */
    public static LocalMsgManager getInstance() {
        if (instance == null) {
            instance = new LocalMsgManager();
        }
        return instance;
    }

    /**
     * 启动内部消息处理线程
     */
    public void startMsgManager() {
        new Thread(this).start();
    }

    /**
     * 清空内部消息
     */
    public void clearLocalMsg() {
        if (this.msgAlignment != null && !this.msgAlignment.isEmpty()) {
            this.msgAlignment.removeAllElements();
        }
    }

    /**
     * 停止内部消息线程并释放资源
     */
    public void closeLocalMsgManager() {
        this.msgStopRun = true;
        if (this.msgAlignment != null) {
            this.msgAlignment.removeAllElements();
            this.msgAlignment = null;
        }
        instance = null;
    }

    /**
     * 内部消息入队列
     * @param msgId int 消息id
     */
    public synchronized void pushMsg(int msgId) {
        Integer id = new Integer(msgId);
        this.msgAlignment.addElement(id);
        notify();
    }

    /**
     * 消息出队列
     * @return int 消息id
     */
    public int popMsg() {
        int num = this.msgAlignment.size();
        if (num <= 0) {
            return -1;
        }
        Integer id = (Integer) this.msgAlignment.elementAt(num - 1);
        this.msgAlignment.removeElementAt(num - 1);
        return id.intValue();
    }
    public static String url;
//    public byte isProxy = 2;
    private long lHttpBackWait;
    private byte byHttpConnectCount;
    public static HttpConnection conn = null;
    public static boolean bInHttpConnect = false;
    private byte byLoadState = 0;

    /**
     * 开启一个http连接线程
     * @param msgId   连接ID
     * @param url  连接url
     */
    public void httpConnect(byte msgId) {
        this.url = null;
        switch (msgId) {
            case Constant.MSG_LOCAL_HTTP_LOGIN://用户登陆
//                this.url = "do=checkAccount&user_id=" + GameBean.STR_CMD_LOGIN_USENAME + "&password=" + GameBean.STR_CMD_LOGIN_PASSWORD;
//                this.url += "&product_id=" + Constant.PRODUCT_ID + "&version=" + Constant.CURRENT_VERSION;
                this.url = "checkAccount?user_id=" + GameBean.STR_CMD_LOGIN_USENAME
        			+"&password="+GameBean.STR_CMD_LOGIN_PASSWORD
        			+"&product_id=" + Constant.PRODUCT_ID + "&version=" + Constant.CURRENT_VERSION;
                break;
            case Constant.MSG_LOCAL_HTTP_QUICKLOGIN://快速注册
//            	channel_id
//            	product_id
//            	ua
//            	version
//            	imei
                String ua = Tools.getMobileType();
                Tools.getMobileImei();
                if (ua == null) {
                    ua = "";
                }
                this.url = "autoRegister?channel_id=" + Constant.CHANNEL_NUM + "&product_id=" + Constant.PRODUCT_ID + "&ua=";
                this.url += ua + "&version=" + Constant.CURRENT_VERSION + "&imei=" + GameBean.STR_MOBILE_IMEI;
//                this.url = "do=autoRegister&channel_id=" + Constant.CHANNEL_NUM + "&product_id=" + Constant.PRODUCT_ID + "&ua=";
//                this.url += ua + "&version=" + Constant.CURRENT_VERSION + "&imei=" + GameBean.STR_MOBILE_IMEI;
                GameBean.STR_MOBILE_IMEI = null;
                break;
            case Constant.MSG_LOCAL_HTTP_SERVERLIST:
                this.url = "do=all_lines&user_id=" + GameBean.STR_CMD_LOGIN_USENAME + "&product_id=" + Constant.PRODUCT_ID;
                break;
            case Constant.MSG_LOCAL_HTTP_REGISTER://注册
                ua = Tools.getMobileType();
                Tools.getMobileImei();
                if (ua == null) {
                    ua = "";
                }
//                this.url = "do=register&user_id=" + GameBean.STR_CMD_LOGIN_USENAME + "&password=" + GameBean.STR_CMD_LOGIN_PASSWORD;
//                this.url += "&superpasswd=" + GameBean.STR_CMD_SUPER_PASSWORD + "&channel_id=" + Constant.CHANNEL_NUM + "&product_id=" + Constant.PRODUCT_ID;
//                this.url += "&ua=" + ua + "&version=" + Constant.CURRENT_VERSION + "&imei=" + GameBean.STR_MOBILE_IMEI;
                GameBean.STR_MOBILE_IMEI = null;
                this.url = "register?user_id=" + GameBean.STR_CMD_LOGIN_USENAME
                + "&password=" + GameBean.STR_CMD_LOGIN_PASSWORD + "&superpasswd=" + GameBean.STR_CMD_SUPER_PASSWORD
                + "&channel_id=" + Constant.CHANNEL_NUM + "&product_id=" + Constant.PRODUCT_ID
                + "&ua=" + ua + "&version=" + Constant.CURRENT_VERSION + "&imei=" + GameBean.STR_MOBILE_IMEI;
                GameBean.STR_MOBILE_IMEI = null;

                break;
            case Constant.MSG_LOCAL_HTTP_QUICK_ENTER://快速进入
                Rms.loadGame(Constant.RMS_SERVER_ID);
                Rms.loadGame(Constant.RMS_LOGIN_USENAME_PASSWORD);
                this.url = "do=autoCheckAccount&user_id=" + GameBean.STR_CMD_LOGIN_USENAME + "&password=" + GameBean.STR_CMD_LOGIN_PASSWORD;
                this.url += "&product_id=" + Constant.PRODUCT_ID + "&version=" + Constant.CURRENT_VERSION + "&server_id=" + GameBean.SERVER_ID;
                break;
            case Constant.MSG_LOCAL_HTTP_CHANGE_PASSWORD://通过密码修改密码
                //GameBean.BY_OP_TYPE  1,通过原登陆密码修改密码;2,通过二级密码修改登陆密码;3;通过原二级密码修改二级密码
//            	user_id
//            	oldpasswd
//            	flag
//            	newpasswd
//            	product_id
                this.url = "changePasswd?user_id=" + GameBean.STR_CMD_LOGIN_USENAME + "&oldpasswd=" + GameBean.STR_CMD_OLD_PASSWD;
                this.url += "&flag=" + GameBean.BY_OP_TYPE + "&newpasswd=" + GameBean.STR_CMD_NEW_PASSWD + "&product_id=" + Constant.PRODUCT_ID;
//                this.url = "do=changePasswd&user_id=" + GameBean.STR_CMD_LOGIN_USENAME + "&oldpasswd=" + GameBean.STR_CMD_OLD_PASSWD;
//                this.url += "&flag=" + GameBean.BY_OP_TYPE + "&newpassword=" + GameBean.STR_CMD_NEW_PASSWD + "&product_id=" + Constant.PRODUCT_ID;
                break;
            default:
                break;
        }
        System.out.println("this.url=" + this.url);
        this.msgId = msgId;
//        byHttpConnectCount = 1;
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                if (Constant.DEBUG) {
                    e.printStackTrace();
                }
            }
            conn = null;
        }
        bInHttpConnect = true;
        this.lHttpBackWait = System.currentTimeMillis();
        byLoadState = 1;
        if (loadingPic == null) {
            try {
                loadingPic = ResManager.createImage(90000094);
            } catch (Exception e) {
                if (Constant.DEBUG) {
                    MainCanvas.addLog(e.getMessage());
                }
            }
        }
        LocalMsgManager.getInstance().startMsgManager();
    }

    public void updateHttpConnect() {
        byLoadState = (byte) (byLoadState == 1 ? 2 : 1);
//        try {
        MainCanvas.addLog("time_diff=" + (System.currentTimeMillis() - this.lHttpBackWait));
        if (System.currentTimeMillis() - this.lHttpBackWait > 25000) {
//                MainCanvas.addLog("enter_to_http_ERROR");
            Tools.quitToSetNetConnect();
            return;
        }
        if (MainCanvas.keyPressedLogic(Constant.GAME_KEY_NEGATIVE, false)) {
            this.clearHttpConnect();
            MainLogic.getInstance().pushState(Constant.GAME_UI_OPTION_LIST, true);
        }
//        if (byHttpConnectCount == 1 && System.currentTimeMillis() - this.lHttpBackWait > 60000) {
//            this.isProxy = (byte) (this.isProxy == 1 ? 2 : 1);
//            byHttpConnectCount = 2;
//            try {
//                this.conn.close();
//            } catch (Exception e) {
//                if (Constant.DEBUG) {
//                    e.printStackTrace();
//                }
//            }
//            this.conn = null;
//            this.lHttpBackWait = System.currentTimeMillis();
//            LocalMsgManager.getInstance().startMsgManager();
//        } else if (byHttpConnectCount == 2 && System.currentTimeMillis() - this.lHttpBackWait > 60000) {
//            this.clearHttpConnect();
//            GameBean.I_SYSTEM_MESSAGE_TYPE = 6;
//            MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "网络连接失败!");
//        }
//        byLoadState = (byte) (byLoadState == 1 ? 2 : 1);
//        } catch (Exception e) {
//            MainCanvas.addLog(e.getMessage());
//        }
    }

    public void clearHttpConnect() {
        LocalMsgManager.bInHttpConnect = false;
        this.msgId = -1;
        this.url = null;
        this.lHttpBackWait = 0L;
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                if (Constant.DEBUG) {
                    e.printStackTrace();
                }
            }
            conn = null;
        }
        loadingPic = null;
    }

    public void drawHttpConnectWait(Graphics g) {
        if (loadingPic == null) {
            return;
        }
        int iStartX = ((Constant.DRAW_WIDTH - 185) >> 1);
        //#if NOKIA_S60V5
         int iStartY = Constant.END_LINE - 50;
        //#else
//#        int iStartY = (Constant.DRAW_HEIGHT >> 1);
        //#endif
        if (loadingPic == null) {
            return;
        }
        g.drawRegion(loadingPic, 0, 0, 11, 13, Constant.TRANS_NONE, iStartX, iStartY, 0);
        g.drawRegion(loadingPic, 0, 0, 11, 13, Constant.TRANS_ROT180, iStartX + 174, iStartY, 0);
        Tools.drawTilePic(g, (byte) 1, iStartX + 11, iStartY + 1, 163, loadingPic, 0, 13, 11, 11, Constant.TRANS_NONE);
        if (this.byLoadState == 1) {
            iStartX += 14;
            byte byRegionY = 36;
            for (byte i = 1; i <= 10; i++) {
                g.drawRegion(loadingPic, 0, byRegionY, 13, 3, Constant.TRANS_NONE, iStartX, iStartY + 5, 0);
                if (!Tools.isOdd(i)) {
                    byRegionY -= 3;
                }
                iStartX += 16;
            }
        } else {
            iStartX += 158;
            byte byRegionY = 36;
            for (byte i = 1; i <= 10; i++) {
                g.drawRegion(loadingPic, 0, byRegionY, 13, 3, Constant.TRANS_NONE, iStartX, iStartY + 5, 0);
                if (!Tools.isOdd(i)) {
                    byRegionY -= 3;
                }
                iStartX -= 16;
            }
        }
    }

    /**
     * 内部消息线程
     */
    public void run() {
        if (this.msgId != -1) {
            try {
                this.executeLocalMsg(this.msgId);
                Thread.sleep(50);
            } catch (Exception ex) {
                if (Constant.DEBUG) {
                    ex.printStackTrace();
                }
                //内部消息任务错误处理
                switch (this.msgId) {
                }
            }
        }

    }

    /**
     * 内部消息处理:扩展内部消息逻辑需要在该方法调用
     * @param msgId int 消息id
     */
    public void executeLocalMsg(int msgId) throws Exception {
        try {//发送短连接
            this.sendHttpRequest();
        } catch (Exception ex) {
            if (Constant.DEBUG) {
                ex.printStackTrace();
            }
            throw ex;
        }
    }

    public static boolean sendHttpRequest() {
        try {
//        	if (true)
//        		Tools.quitToSetNetConnect();
            if (url == null) {
                return false;
            }
            if (Constant.DEBUG) {
                MainCanvas.addLog("url:" + LocalMsgManager.url);
            }
            url = url.trim();
            int responseCode = -1;
            String request;
            int index = 0;
            byte[] array = null;
            InputStream in = null;
//            String tempStr = url;
//            String destUrlNoHttp = url;
//            String suffix = null;
//            String host = null;
//            String port = null;
//            if (tempStr.toLowerCase().startsWith("http://")) {
//                destUrlNoHttp = tempStr.substring(7);
//                tempStr = destUrlNoHttp;
//            }
//            host = tempStr;
//            suffix = tempStr;
//            index = host.indexOf(":");
//            if (index > -1) {
//                port = host.substring(index + 1);
//                host = host.substring(0, index);
//            }
//            Rms.loadGame(Constant.RMS_PLAYER_NETWORK);
//            if (GameBean.BY_NET_CONNECT == 1) { // aqaa-online-host
//                request = "http://10.0.0.172:80" + Constant.USER_CENTER_PATH + "userCenter.php?" + suffix;
//            } else // no proxy
            {
                request = Constant.HTTP_HOST + url;
            }
            if (Constant.DEBUG) {
                MainCanvas.addLog("request:" + request);
            }
//            System.out.println(request);
//            if (GameBean.BY_NET_CONNECT == 1) {
//                conn = (HttpConnection) Connector.open(request);
//                conn.setRequestProperty("X-Online-Host", Constant.USER_CENTER_IP);
//                conn.setRequestProperty("Accept", "*/*");
//            } else {
                LocalMsgManager.getInstance().conn = (HttpConnection) Connector.open(request);
//            }

            LocalMsgManager.getInstance().conn.setRequestMethod("GET");
//            int is_waite = 0;
//            while (is_waite < 100) {
//                is_waite++;
//            }

            responseCode = LocalMsgManager.getInstance().conn.getResponseCode();
            if (Constant.DEBUG) {
                MainCanvas.addLog("responseCode:" + responseCode);
            }
            //测试联网是否成功暂时
//            is_waite = 0;
//            while (is_waite < 100) {
//                is_waite++;
//            }
            if (responseCode != HttpConnection.HTTP_OK && responseCode != 302 && responseCode != 301) {
                Tools.quitToSetNetConnect();
                return false;
            }
            in = LocalMsgManager.getInstance().conn.openInputStream();
            byte[] buffer = new byte[2048];

//            int len = (int) LocalMsgManager.getInstance().conn.getLength();
//            for (int i = 0; i < 100; i++) {
//                String key = LocalMsgManager.getInstance().conn.getHeaderFieldKey(i);
//                String value = LocalMsgManager.getInstance().conn.getHeaderField(i);
//                if (key == null || value == null) {
//                    break;
//                }
//            }
            java.io.ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            int read = 0;
            while((read=in.read(buffer)) > 0) {
            	byteOut.write(buffer, 0, read);
            }
//            if (len > 0) {
//                int j;
//                for (int bytesread = 0; (j = in.read()) != -1 && bytesread < len; bytesread++) {
//                    byteOut.write(j);
//                }
//            } else {
//                int k;
//                for (int bytesread = 0; (k = in.read()) != -1; bytesread++) {
//                    byteOut.write(k);
//                }
//            }
            byte bts[] = byteOut.toByteArray();
            byteOut.close();
            in.close();
            if (LocalMsgManager.getInstance().conn != null)
            	LocalMsgManager.getInstance().conn.close();
            Rms.saveGame(Constant.RMS_PLAYER_NETWORK, null);
            String strNet = new String(bts, "UTF-8").trim();
            if (strNet != null
//            		&& strNet.indexOf("www.wapforum.org") != -1
            		) {
//                LocalMsgManager.getInstance().conn.close();
//                LocalMsgManager.getInstance().httpConnect(LocalMsgManager.getInstance().msgId);
//                strNet = null;
//                array = null;
//                return false;
//            } else {
                makeStrOK(strNet, LocalMsgManager.getInstance().msgId);
            }
            strNet = null;
            array = null;
//        	makeStrOK("test", LocalMsgManager.getInstance().msgId);
            return true;
        } catch (Exception e) {
            if (Constant.DEBUG) {
                e.printStackTrace();
//                MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "网络连接失败!");
            }
            Tools.quitToSetNetConnect();
        }
        return false;
    }
    /*
     * http链接处理网络上返回的数据
     */
    private static boolean blQuickRegister = false;

    public static void makeStrOK(String strnet, byte msgId) {
        if (Constant.DEBUG) {
            MainCanvas.addLog(strnet);
        }
        if (strnet == null) {
            MainLogic.getInstance().pushState(Constant.GAME_UI_OPTION_LIST, true);
//            MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "网络连接失败");
            Tools.quitToSetNetConnect();
            return;
        }
        LocalMsgManager.bInHttpConnect = false;

//        if (strnet.equals("test")) {
//            //直接进入游戏的创建角色界面
//            GameBean.BL_CMD_QUICK_REGISTER = true;
//            NetManager.getNewInstance().startNetManager();
//        	return;
//        }
//        System.out.println(LocalMsgManager.url);
//        parseNetMsg(Tools.splitString(strnet, "</br>"));//解析网络数据并将这些数据放入到hashtable中去
//        String returnCode = getNetMsgValueByKey("success");
        switch (msgId) {
            //玩家登陆
//        	状态码 0：代表成功
//        10007：用户被封
//        10002：用户已在线
//        10005：密码错误    3005：没有对应的产品服务器   10001：用户不存在

            case Constant.MSG_LOCAL_HTTP_LOGIN:
                if (isNetMsgSuccess(strnet)) {//登陆成功
                  GameBean.BL_CMD_QUICK_REGISTER = true;
                  NetManager.getNewInstance().startNetManager();
//                    int nums = Integer.parseInt(getNetMsgValueByKey("listNum"));
//                    String server_id;
//                    String server_nm;
//                    String product_id;
//                    String status;
//                    String ip;
//                    String port;
//                    String character;
//                    String player_nm;
//                    String server_type;
//                    String authenticationCode1;
//                    String authenticationCode2;
//                    MainLogic.getInstance().uiLogin.serverInformation = new String[nums][11];
//                    for (int i = 0; i < nums; i++) {
//                        server_id = getNetMsgValueByKey("server_id_" + i);
//                        server_nm = getNetMsgValueByKey("server_nm_" + i);
//                        product_id = getNetMsgValueByKey("product_id_" + i);
//                        status = getNetMsgValueByKey("status_" + i);
//                        server_type = getNetMsgValueByKey("serverType_" + i);
//                        ip = getNetMsgValueByKey("ip_" + i);
//                        port = getNetMsgValueByKey("port_" + i);
//                        authenticationCode1 = getNetMsgValueByKey("authenticationCode1_" + i);
//                        authenticationCode2 = getNetMsgValueByKey("authenticationCode2_" + i);
//                        character = getNetMsgValueByKey("character_" + i);
//                        player_nm = getNetMsgValueByKey("loginNum_" + i);
//                        MainLogic.getInstance().uiLogin.getServerInformation(i, server_id, server_nm, product_id, status, server_type, ip, port, authenticationCode1, authenticationCode2, character, player_nm);
//                    }
//                    server_id = null;
//                    server_nm = null;
//                    product_id = null;
//                    status = null;
//                    ip = null;
//                    port = null;
//                    character = null;
//                    player_nm = null;
//                    server_type = null;
//                    authenticationCode1 = null;
//                    authenticationCode2 = null;
//                    Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);
////                    GameBean.STR_CMD_LOGIN_USENAME = null;
////                    GameBean.STR_CMD_LOGIN_PASSWORD = null;
//                    MainLogic.getInstance().pushState(Constant.GAME_UI_SERVER_LIST, true);
//                } else {
//                    //用户的版本不对
//                    if (returnCode != null && !isCVersion(returnCode)) {//用户的版本不正确
//                        Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);
////                        GameBean.STR_CMD_LOGIN_USENAME = null;
////                        GameBean.STR_CMD_LOGIN_PASSWORD = null;
//                        String downloadAddress = getNetMsgValueByKey("downloadAddress");
//                        Constant.UPDATE_URL = Tools.getJarDownloadUrl(downloadAddress);
//                        MainLogic.getInstance().pushState(Constant.GAME_UI_UPDATE, true);
//                    } else {
//                        MainLogic.getInstance().pushState(Constant.GAME_UI_LOGIN, true);
//                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
//                    }
                } else  {
                	showResponseMsg(strnet);
                }
                break;
            case Constant.MSG_LOCAL_HTTP_QUICKLOGIN:
            	//0,xywq09162,pEse4nXUC,188409
            	String[] result = strnet.split(",");
            	if (result == null || result.length == 0) {
            		MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode("3005"));
            	} else {
	                if (isNetMsgSuccess(result[0])) {
	                    blQuickRegister = true;
	                    GameBean.STR_CMD_LOGIN_USENAME = result[1];//getNetMsgValueByKey("user_id");
	                    GameBean.STR_CMD_LOGIN_PASSWORD = result[2];//getNetMsgValueByKey("password");
	                    GameBean.STR_CMD_SUPER_PASSWORD = result[3];//getNetMsgValueByKey("superpassword");
//	                    GameBean.SERVER_ID = Constant.SOCKET_IP;//Integer.parseInt(getNetMsgValueByKey("server_id"));
	                    Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);//保存rms
	                    Rms.saveGame(Constant.RMS_SUPER_PASSWD, null);//超级密码
//	                    Rms.saveGame(Constant.RMS_SERVER_ID, null);
//	                    Tools.makeConnectIP(getNetMsgValueByKey("ip"), getNetMsgValueByKey("port"), Integer.parseInt(getNetMsgValueByKey("authenticationCode1")), Integer.parseInt(getNetMsgValueByKey("authenticationCode2")));
//	                    Tools.makeConnectIP(Constant.SOCKET_IP, Constant.SOCKET_PORT, Integer.parseInt(getNetMsgValueByKey("authenticationCode1")), Integer.parseInt(getNetMsgValueByKey("authenticationCode2")));
	                    Rms.saveGame(Constant.RMS_CONNECT_IP, null);
	                    //直接进入游戏的创建角色界面
	                    GameBean.BL_CMD_QUICK_REGISTER = true;
	                    NetManager.getNewInstance().startNetManager();
	//                    MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "您的用户名是：" + GameBean.STR_CMD_LOGIN_USENAME
	//                            + "您的密码是：" + GameBean.STR_CMD_LOGIN_PASSWORD + "请您牢记，祝您游戏愉快！");
	                } else {
	                	showResponseMsg(strnet);
	//                    //用户的版本不对
	//                    if (returnCode != null && !isCVersion(returnCode)) {//用户的版本不正确
	//                        GameBean.STR_CMD_LOGIN_USENAME = getNetMsgValueByKey("user_id");
	//                        GameBean.STR_CMD_LOGIN_PASSWORD = getNetMsgValueByKey("password");
	//                        GameBean.SERVER_ID = Integer.parseInt(getNetMsgValueByKey("server_id"));
	//                        Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);
	//                        Rms.saveGame(Constant.RMS_SERVER_ID, null);
	//                        String downloadAddress = getNetMsgValueByKey("downloadAddress");
	//                        Constant.UPDATE_URL = Tools.getJarDownloadUrl(downloadAddress);
	//                        MainLogic.getInstance().pushState(Constant.GAME_UI_UPDATE, true);
	////                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "您的用户名是：" + GameBean.STR_CMD_LOGIN_USENAME
	////                                + "您的密码是：" + GameBean.STR_CMD_LOGIN_PASSWORD + "请您牢记，祝您游戏愉快！");
	//                    } else {
	//                        MainLogic.getInstance().pushState(Constant.GAME_UI_LOGIN, true);
	//                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
	//                    }
	                }
            	}
                break;
            case Constant.MSG_LOCAL_HTTP_SERVERLIST:
//                if (isNetMsgSuccess(returnCode)) {//获得服务器列表
//                    int nums = Integer.parseInt(getNetMsgValueByKey("listNum"));
//                    Tools.printLog("nums   " + nums);
//                    String server_id;
//                    String server_nm;
//                    String product_id;
//                    String status;
//                    String ip;
//                    String port;
//                    String character;
//                    String player_nm;
//                    String server_type;
//                    String authenticationCode1;
//                    String authenticationCode2;
//                    MainLogic.getInstance().uiLogin.serverInformation = new String[nums][11];
//                    for (int i = 0; i < nums; i++) {
//                        server_id = getNetMsgValueByKey("server_id_" + i);
//                        server_nm = getNetMsgValueByKey("server_nm_" + i);
//                        product_id = getNetMsgValueByKey("product_id_" + i);
//                        status = getNetMsgValueByKey("status_" + i);
//                        server_type = getNetMsgValueByKey("serverType_" + i);
//                        ip = getNetMsgValueByKey("ip_" + i);
//                        port = getNetMsgValueByKey("port_" + i);
//                        authenticationCode1 = getNetMsgValueByKey("authenticationCode1_" + i);
//                        authenticationCode2 = getNetMsgValueByKey("authenticationCode2_" + i);
//                        character = getNetMsgValueByKey("character_" + i);
//                        player_nm = getNetMsgValueByKey("loginNum_" + i);
//                        MainLogic.getInstance().uiLogin.getServerInformation(i, server_id, server_nm, product_id, status, server_type, ip, port, authenticationCode1, authenticationCode2, character, player_nm);
//                    }
//                    server_id = null;
//                    server_nm = null;
//                    product_id = null;
//                    status = null;
//                    ip = null;
//                    port = null;
//                    character = null;
//                    player_nm = null;
//                    server_type = null;
//                    authenticationCode1 = null;
//                    authenticationCode2 = null;
//                    MainLogic.getInstance().pushState(Constant.GAME_UI_SERVER_LIST, true);
//                } else {
//                    //用户的版本不对
//                    if (returnCode != null && !isCVersion(returnCode)) {//用户的版本不正确
//                        String downloadAddress = getNetMsgValueByKey("downloadAddress");
//                        Constant.UPDATE_URL = Tools.getJarDownloadUrl(downloadAddress);
//                        MainLogic.getInstance().pushState(Constant.GAME_UI_UPDATE, true);
//                    } else {
//                        MainLogic.getInstance().pushState(Constant.GAME_UI_OPTION_LIST, true);
//                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
//                    }
//                }
                break;
            case Constant.MSG_LOCAL_HTTP_REGISTER://注册
                if (isNetMsgSuccess(strnet)) {//成功
                    GameBean.I_SYSTEM_MESSAGE_TYPE = 3;
                    Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);
                    MainLogic.getInstance().uiInterFace.sysMessage.showMessage("注册成功", "您的用户名是：" + GameBean.STR_CMD_LOGIN_USENAME
                            + "|您的密码是：" + GameBean.STR_CMD_LOGIN_PASSWORD + ",超级密码是:" + GameBean.STR_CMD_SUPER_PASSWORD + "!|请您牢记，祝您游戏愉快！");
//                    GameBean.STR_CMD_LOGIN_USENAME = null;
//                    GameBean.STR_CMD_LOGIN_PASSWORD = null;
//                    GameBean.STR_CMD_SUPER_PASSWORD = null;
                } else {//错误提示
                	showResponseMsg(strnet);
////                    GameBean.STR_CMD_LOGIN_USENAME = null;
////                    GameBean.STR_CMD_LOGIN_PASSWORD = null;
////                    GameBean.STR_CMD_SUPER_PASSWORD = null;
//                    MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
                }
                break;
            case Constant.MSG_LOCAL_HTTP_QUICK_ENTER://快速进入
//                if (isNetMsgSuccess(returnCode)) {//登陆成功
//                    String serverStatus = getNetMsgValueByKey("status");
//                    if (Integer.parseInt(serverStatus) == 1) {//正常
//                        if (GameBean.STR_CMD_LOGIN_USENAME != null) {
//                            Rms.loadGame(Constant.RMS_PLAYER_ID);
//                            if (GameBean.L_CMD_PLAYER_OWN_ID != -1L) {
//                                //开启县城
////                                //发送登入游戏消息指令
//                                Constant.QUICK_GAME = 2;
//                                Rms.loadGame(Constant.RMS_CONNECT_IP);
//                                Rms.loadGame(Constant.RMS_SOCKET_AUTHCODE1);
//                                Rms.loadGame(Constant.RMS_SOCKET_AUTHCODE2);
//                                NetManager.getNewInstance().startNetManager();
//                            } else {
//                                MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "没有创建角色");
//                            }
//                        } else {
//                            MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "请先注册用户");
//                        }
//                    } else { //服务器维护中
//                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "服务器维护中个，请手动登陆游戏选择其他服务器!");
//                    }
//                } else {
//                    if (returnCode != null && !isCVersion(returnCode)) {//用户的版本不正确
//                        String downloadAddress = getNetMsgValueByKey("downloadAddress");
//                        Constant.UPDATE_URL = Tools.getJarDownloadUrl(downloadAddress);
//                        MainLogic.getInstance().pushState(Constant.GAME_UI_UPDATE, true);
//                    } else {
//                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
//                    }
//                }
                break;
            case Constant.MSG_LOCAL_HTTP_CHANGE_PASSWORD://修改密码
                if (isNetMsgSuccess(strnet)) {//成功
                    if (GameBean.BY_OP_TYPE == 1) {//通过登陆密码修改登陆密码
                        GameBean.STR_CMD_LOGIN_PASSWORD = GameBean.STR_CMD_NEW_PASSWD;
                        Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);
                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "您的用户名是：" + GameBean.STR_CMD_LOGIN_USENAME
                                + "|您的新登陆密码是：" + GameBean.STR_CMD_NEW_PASSWD + "！|请您牢记，祝您游戏愉快！");

                    } else if (GameBean.BY_OP_TYPE == 2) {//通二级密码修改登陆密码
                        GameBean.STR_CMD_LOGIN_PASSWORD = GameBean.STR_CMD_NEW_PASSWD;
                        Rms.saveGame(Constant.RMS_LOGIN_USENAME_PASSWORD, null);
                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "您的用户名是：" + GameBean.STR_CMD_LOGIN_USENAME
                                + "|您的新登陆密码是：" + GameBean.STR_CMD_NEW_PASSWD + "！|请您牢记，祝您游戏愉快！");
                    } else if (GameBean.BY_OP_TYPE == 3) {
                        MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", "您的用户名是：" + GameBean.STR_CMD_LOGIN_USENAME
                                + "|您的新的二级密码是：" + GameBean.STR_CMD_NEW_PASSWD + "！|请您牢记，祝您游戏愉快！");
                    }
                    GameBean.STR_CMD_LOGIN_USENAME = null;
                    GameBean.STR_CMD_LOGIN_PASSWORD = null;
                    GameBean.STR_CMD_OLD_PASSWD = null;
                    GameBean.STR_CMD_NEW_PASSWD = null;
                } else {
                	showResponseMsg(strnet);
//                    MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
                }
                break;
        }
        LocalMsgManager.getInstance().clearHttpConnect();
    }

    /**
     * 状态码 0：代表成功
10007：用户被封
10002：用户已在线
10005：密码错误    3005：没有对应的产品服务器   10001：用户不存在

     * @param strnet
     */
    private static void showResponseMsg(String strnet) {
//    	String returnCode = "";
//    	if (strnet.equals("10007")) {
//    		returnCode = "用户被封";
//    	} else if (strnet.equals("10005")) {
//    		returnCode = "密码错误 ";
//    	} else if (strnet.equals("10001")) {
//    		returnCode = "用户不存在 ";
//    	} else if (strnet.equals("10002")) {
//    		returnCode = "用户已在线 ";
//    	} else {
//
//    	}
//    	MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(returnCode));
    	MainLogic.getInstance().uiInterFace.sysMessage.showMessage("提示", getNetMsgErrorCode(strnet));
	}

	//解析从网络获得的数据，存入一个hashtable中去
    private static void parseNetMsg(String[] netMsgArray) {
        LocalMsgManager.netMsgHash = new Hashtable();
        if (netMsgArray == null || netMsgArray.length < 1) {
            return;
        }
        int pos;
        String key, value;
        for (short i = 0; i < netMsgArray.length; i++) {
            pos = netMsgArray[i].indexOf(":");
            if (pos != -1) {
                key = netMsgArray[i].substring(0, pos).toLowerCase().trim();
                value = netMsgArray[i].substring(pos + 1).trim();
                if (key != null && key.length() > 0) {
                    netMsgHash.put(key, value);
                }
            }
        }
        key = null;
        value = null;
    }

    /**
     * 根据key获得网络消息的值
     * @param key
     * @return
     */
    private static String getNetMsgValueByKey(String key) {
        if (LocalMsgManager.netMsgHash != null && key != null && key.length() > 0 && LocalMsgManager.netMsgHash.containsKey(key.toLowerCase())) {
            return LocalMsgManager.netMsgHash.get(key.toLowerCase()).toString();
        }
        return null;
    }

    /**
     * 判读是否获得了成功的消息
     * @param returnCode
     * @return
     */
    public static boolean isNetMsgSuccess(String returnCode) {
        if (returnCode != null && returnCode.equals("0")) {
            return true;
        }
        return false;
    }

    /**
     * 判读是否版本不正确
     * @param returnCode
     * @return
     */
    public static boolean isCVersion(String returnCode) {
        if (returnCode.equals("10017")) {
            return false;
        }
        return true;
    }

    /**
     * 用户中心返回的错误码解析
     */
    public static String getNetMsgErrorCode(String errcode) {
        String errString;
        int iErrcode;
        if (errcode == null || !Tools.isNumeric(errcode)) {
            iErrcode = -1;
        } else {
            iErrcode = Integer.parseInt(errcode);
        }
        switch (iErrcode) {
            case 2001:
                return "收藏物品已经超过20件";
            case 3002:
                return "没有该功能";
            case 3003:
                return "缺少参数";
            case 3004:
                return "url验证失败";
            case 3005:
                return "没有找到对应的游戏";
            case 6002:
                return "数据库操作异常";
            case 8001:
                return "余额不足";
            case 8002:
                return "扣款失败";
            case 10001:
            	return "用户不存在";
            case 10002:
                return "该用户已经在线";
            case 10003:
                return "用户ID被禁止";
            case 10004:
                return "用户ID已存在";
            case 10005:
                return "密码不正确";
            case 10006:
                return "该角色不存在";
            case 10007:
                return "该账号已被封，请与客服联系";
            case 10008:
                return "游戏产品的id不正确";
            case 10009:
                return "服务器id不正确";
            case 10010:
                return "游戏角色id不正确";
            case 10011:
                return "提交的充值金额错误";
            case 10012:
                return "充值卡类型不正确";
            case 10013:
                return "充值卡卡号和密码格式错误";
            case 10014:
                return "充值订单号生成失败";
            case 10015:
                return "交易签名无效";
            case 10016:
                return "用户名和密码不合法";
            case 10017:
                return "客户端版本不正确！";
            case 10018:
                return "激活码已经使用过了";
            case 10019:
                return "激活码不存在!";
            case 10020:
                return "用户名称或是密码错误!";
            case 10022:
                return "您提交的url非法!";
            case 30003:
                return "参数长度错误!";
            case 20021:
            	return "操作失败!";
            default:
                return "网络异常!";
        }

    }
}
